package com.duolaibei.pay.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.duolaibei.pay.R;
import com.duolaibei.pay.base.BaseActivity;
import com.duolaibei.pay.bean.PayOrder;
import com.duolaibei.pay.http.BaseCallback;
import com.duolaibei.pay.http.HttpRetrofit;
import com.duolaibei.pay.http.ParamsUtils;
import com.duolaibei.pay.http.Result;
import com.duolaibei.pay.http.api.ApiName;
import com.duolaibei.pay.http.api.SerivceApi;
import com.duolaibei.pay.utils.Dialogutils;
import com.duolaibei.pay.utils.SharedPreferencesUtils;
import com.duolaibei.pay.utils.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by wangshuo on 2017/7/10.
 */

public class BrushCardManagerActivity extends BaseActivity implements Dialogutils.OnDismissListener, BaseCallback<Result<PayOrder>> {
    @Bind(R.id.detail_layout)
    LinearLayout detailLayout;
    @Bind(R.id.reimburse_layout)
    LinearLayout reimburseLayout;
    @Bind(R.id.pre_authorization_layout)
    LinearLayout preAuthorizationLayout;
    @Bind(R.id.system_manager_layout)
    LinearLayout systemManagerLayout;
    @Bind(R.id.settlement_layout)
    LinearLayout settlementLayout;

    @Bind(R.id.cashier_top)
    LinearLayout cashierTop;
    static final String MANAGE = "7";//系统管理
    static final String PREAUTH = "3";//预授权
    static final String CANCEL = "1";//交易撤销
    static final String ACCOUNTS = "4";
    Intent intent;
    private String certificateNumber;//凭证号；

    public static final int INDEX_TYPE = 1;

    @Override
    public int getContentViewResId() {
        return R.layout.activity_brush_card_managr;
    }

    @Override
    public void intiViews() {
        detailLayout.setOnClickListener(this);
        reimburseLayout.setOnClickListener(this);
        preAuthorizationLayout.setOnClickListener(this);
        systemManagerLayout.setOnClickListener(this);
        settlementLayout.setOnClickListener(this);
        intent = initIntent();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id = view.getId();
        switch (id) {
            case R.id.detail_layout:
                Intent intent1 = new Intent(BrushCardManagerActivity.this, TransactionActivity.class);
                intent1.putExtra("pay_type", INDEX_TYPE);
                startActivity(intent1);
                break;
            case R.id.reimburse_layout:
                Dialogutils.showDialog(this, "", this);
                break;
            case R.id.pre_authorization_layout:
                intent.putExtra("transName", PREAUTH);
                intent.putExtra("amount", "1");
                startActivityForResult(intent, 2);//2、预授权
                break;
            case R.id.system_manager_layout:
                intent.putExtra("transName", MANAGE);
                startActivityForResult(intent, 0);
                break;
            case R.id.settlement_layout:
                intent.putExtra("transName", ACCOUNTS);
                startActivityForResult(intent, 0);
                break;
        }
    }

    //抽取各个交易类型公共的字段
    private Intent initIntent() {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.shengpay.smartpos.shengpaysdk",
                "com.shengpay.smartpos.shengpaysdk.activity.MainActivity"));
        intent.putExtra("appId", getPackageName());
        return intent;
    }


    @Override
    public String setTitle() {
        return getString(R.string.brush_card_manager);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }

    @Override
    public void onConfirm(String ContentTxet) {
        if (TextUtils.isEmpty(ContentTxet)) {
            return;
        }
        certificateNumber = ContentTxet;
        intent.putExtra("transName", CANCEL);
        intent.putExtra("oldTraceNo", ContentTxet);
        startActivityForResult(intent, 1);//1、交易撤销
    }

    @Override
    public void onCancel() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {//交易撤销
            switch (resultCode) {
                case Activity.RESULT_OK:
                    ToastUtils.showToast(this, "交易撤销成功", 3000);
                    HttpRetrofit.getInstance().getInterfaceApi(SerivceApi.class).refund(SharedPreferencesUtils.getAppid(), ApiName.RE_FUND, ParamsUtils.BrushRefund(certificateNumber), certificateNumber
                            , "", SharedPreferencesUtils.getWorkMenId()).enqueue(this);
                    break;
                case Activity.RESULT_CANCELED:
                    String reason = data.getStringExtra("reason");
                    ToastUtils.showToast(this, "交易取消", 3000);
                    break;
            }
        } else if (requestCode == 2) {//预授权
            switch (resultCode) {
                case Activity.RESULT_OK:
                    Toast.makeText(BrushCardManagerActivity.this, "预授权成功", Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    String reason = data.getStringExtra("reason");
                    ToastUtils.showToast(this, "交易取消", 3000);
                    break;
            }
        }
    }

    @Override
    public void onResponse(Call<Result<PayOrder>> call, Response<Result<PayOrder>> response) {
        if (response.code() == 200) {
            if (response.body().getStatus().equals("1")) {
                ToastUtils.showToast(this, "订单状态已修改", 5000);
            } else {
                ToastUtils.showToast(this, response.body().getInfo(), 5000);
            }
        }
    }

    @Override
    public void onFailure(Call<Result<PayOrder>> call, Throwable t) {
        Log.e("onFailure", "error message:" + t.getMessage());
    }

}
